<?php
	class sql{
		// 初始化变量
		private $host;
		private $user;
		private $pass;
		private $data;
		private $charset;
		

		public function __construct(array $info = array()){
			// 变量初始值赋值
			$this->host = isset($info['HOST']) ? $info['HOST'] : 'localhost';
			$this->user = isset($info['USER']) ? $info['USER'] : 'root';
			$this->pass = isset($info['PASS']) ? $info['PASS'] : 'aqs1634077';
			$this->data = isset($info['DATA']) ? $info['DATA'] : 'essaysystem';
			$this->charset = isset($info['CHARSET']) ? $info['CHARSET'] : 'utf8';

			$this->db_connect();
		}

		public $conn;
		private function db_connect(){
			// 链接数据库
			$this->conn = @new PDO("mysql:host={$this->host};dbname={$this->data}","{$this->user}","{$this->pass}");

			// 设置字符集编码
			$this->conn->exec("set names {$this->charset}");
		}

		// 封装多条或单条数据的查询
		public function db_select($sql, $more = false, $key = '', $startPage = '', $endPage = '', $where = ''){
			
			$smit = $this->conn->prepare($sql);  //准备预处理

			// 预处理赋值
			if(strlen($key) > 0){

				$key = "%{$key}%";
				$smit->bindParam(1, $key, PDO::PARAM_STR);
				$smit->bindParam(2, $startPage, PDO::PARAM_INT);
				$smit->bindParam(3, $endPage, PDO::PARAM_INT);
			}else{

				if(strlen($where) > 0){

					$smit->bindParam(1, $where, PDO::PARAM_INT);
				}else{

					$smit->bindParam(1, $startPage, PDO::PARAM_INT);
					$smit->bindParam(2, $endPage, PDO::PARAM_INT);
				}

			}
			$smit->execute();

			if($more){

				return $smit->fetchAll(PDO::FETCH_ASSOC); // 返回多条数据
			}else{

				return $smit->fetch(PDO::FETCH_ASSOC);  // 返回单条数据
			}
			$smit = null;
		}

		// 受影响的条数
		public function db_select_affect($sql, $users, $pass = '', $more = false){

			$smit = $this->conn->prepare($sql);

			$users = "{$users}";
			$pass = "{$pass}";

			if($more){

				$smit->bindParam(1, $users, PDO::PARAM_STR);
				$smit->bindParam(2, $pass, PDO::PARAM_STR);
			}else{

				$smit->bindParam(1, $users, PDO::PARAM_STR);
			}
			$smit->execute();
			return $smit->rowCount();
		}


		// 封装查询数据总条数
		public function db_select_count($sql, $key = ''){

			$smit = $this->conn->prepare($sql);

			if(strlen($key) > 0){

				$key = "%{$key}%";
				$smit->bindParam(1, $key, PDO::PARAM_STR);
			}
			$smit->execute();

			return $smit->fetch(PDO::FETCH_ASSOC)['aggregate'];

			$smit = null;
		}


		// 删除原图片 img
		public function db_raw_img($table, $id){

			$sql = "select img from {$table} where id=?";

			$smit = $this->conn->prepare($sql);

			if(strlen($id) > 0){

				$smit->bindParam(1, $id, PDO::PARAM_INT);
			}

			$smit->execute();

			$smit = null;

			$imgArr = $this->db_select($sql, false, null, null, null, $id);

			if(strlen($imgArr['img']) > 0){
				if(file_exists($imgArr['img'])){
					
					unlink($imgArr['img']);
				}	
			}
		}


		// 删除指定的SQL语句以及数据记录
		public function db_delete($table, $id){

			// 构建SQL语句，发送执行
			$sql = "delete from {$table} where id=?";

			$smit = $this->conn->prepare($sql);

			$smit->bindParam(1, $id, PDO::PARAM_INT);

			return $smit->execute();
			$smit = null;
		}


		// 修改指定的SQL语句及数据记录
		public function db_update($table, $val, $id){

			$sql = "update {$table} set {$val} where id=?";

			$smit = $this->conn->prepare($sql);

			$smit->bindParam(1, $id, PDO::PARAM_INT);
			return $smit->execute();

			$smit = null;
		}


		// 新增一条SQL记录
		public function db_insert($sql){

			$smit = $this->conn->prepare($sql);

			return $smit->execute();
			$smit = null;
		}

		public function db_insert_id(){

			return $this->conn->lastInsertId();
		}
	}
?>